<?php
	
	namespace Aspic\Form\Element;
	use Aspic\Security\Security as S;
	use Aspic as A;

	/**
	* Basic form element with lineStart and lineEnd
	*/
	abstract class AbstractBasicElement extends AbstractElement {
		/**
		* @var array This is an array of attributes for the "main" HTML part 
		* like for example the input tag for a textbox, or a "div" tag that surround radios in RadioGroup class
		* This is not always the "DataTag" (input, select, textarea, ..) that's why the name attribute is a property
		* in DataElement class and is not in the attributes array
		*/
		protected $attributes;
		
		protected $_lineStart;
		protected $_lineEnd;
		
		public function __construct(array $options = array(), array $attributes = array()) {
			parent::__construct($options);
			
			$this->attributes = array();
			$this->opt('lineStart', '', true);
			$this->opt('lineEnd', '', true);
			
			$this->setAttributes($attributes);
		}
		
		public function setAttributes(array $attributes) {
			$this->attributes = array_merge($this->attributes, $attributes);
		}
		
		
		public function attr($attrName, $value = null, $setOnlyIfNotSet = false) {
			if($value !== null) {
				if (!$setOnlyIfNotSet OR is_null($this->attr($attrName))) {
					$this->attributes[$attrName] = $value;
				}
			}
			else {
				if(isset($this->attributes[$attrName])) {
					return $this->attributes[$attrName];
				}
				else {
					return null;
				}
			}
		}
		
		/**
		 * Remove an attribute
		 * @param type $attrName
		 * @return boolean
		 */
		public function removeAttr($attrName) {
			if(isset($this->attributes[$attrName])) {
				unset($this->attributes[$attrName]);
				
				return true;
			}
			else {
				return false;
			}
		}
		
		/**
		 * Remove a list of attributes
		 * @param array $attributes
		 * @return bool
		 */
		public function removeAttrs(array $attributes) {
			$res = true;
			
			foreach ($attributes as $attrName) {
				$res &= $this->removeAttr($attrName);
			}
			
			return $res;
		}
		
		protected function getAttrsStr() {
			return A\UString::implodeHtmlAttributes($this->attributes);
		}
		
		public function out() {
			$content = $this->_lineStart . $this->outElement() . $this->_lineEnd;
			
			return $content;
		}
		
		/**
		* The content of the element
		*/
		abstract public function outElement();
		
	}
	
?>